package com.why.blog.admin.login;

import com.why.blog.common.R;
import com.why.blog.common.ResponseCode;
import com.why.blog.entity.VO.HaloUserVO;
import com.why.blog.service.HaloUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/auth")
public class AuthController {
    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;
    @Autowired
    private HaloUserService userService;

    @PostMapping("/register")
    public R registerUser(@RequestBody Map<String, String> register) {
        HaloUserVO uvo = new HaloUserVO();
        uvo.setUserId(UUID.randomUUID().toString().replaceAll("-", ""));
        uvo.setUserName(register.get("userName"));
        uvo.setUserPass(bCryptPasswordEncoder.encode(register.get("password")));
        uvo.setRole("ROLE_USER");
        Integer res = userService.saveUser(uvo);
        if (res > 0) {
            return R.getInstance(ResponseCode.RESPONSE_SUCCESS.getCode(), ResponseCode.RESPONSE_SUCCESS.getMsg(), uvo);
        }
        return R.getInstance(ResponseCode.PARAMETER_ERROR.getCode(), ResponseCode.PARAMETER_ERROR.getMsg());
    }
}
